package flex.cc.drive.service.impl;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import flex.cc.PublicUtil;
import flex.cc.common.utils.SendSmsUtil;
import flex.cc.sms.dto.SmsInfoEntity;
import flex.cc.sms.service.SmsInfoService;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.service.DictCodeService;

/**
*
* @author WANGCONG
* 2022年1月18日上午9:17:37
* @desc 代驾服务短信发送接口
*/
@Service
public class DriverSmsService {

	private static final Logger logger = LoggerFactory.getLogger(DriverSmsService.class);
	@Resource
	private SmsInfoService smsInfoService;
	@Resource
    private DictCodeService dictCodeService;
    @Resource
    private SendSmsUtil sendSmsUtil;
    
	/**
	*
	* @author WANGCONG
	* 2022年1月18日上午9:17:42
	* @param driverStatus：代驾环节，001-派单成功、002-司机已到达
	* @desc 代驾短信发送
	*  map参数：driverStatus-代驾环节、driverName-司机名称、phoneNumber-司机电话、orderNo-预约编号
	*/
	@Async
	public void sendDriverSms(Map<String, String> params) {
		logger.info("--------------------进入代驾服务短信发送接口--------------------");
		String driverStatus = params.get("driverStatus");
		if("001".equals(driverStatus) || "002".equals(driverStatus)){
			DictCodeEntity smsTemplate = dictCodeService.searchAllCodeByTypeCode("smsTemplate", driverStatus, 0, null); // 短信模版
			if(PublicUtil.isNotEmpty(smsTemplate)){
				String templateId = smsTemplate.getValue(); // 腾讯云模版id
	            String switchStatus = smsTemplate.getValue1();// 短信模版的value1字段表示该模版的开关状态，即0-关，1-开
	            if("1".equals(switchStatus)){
	            	String phoneNumber = params.get("phoneNumber"); //客户电话
	            	String driverName = params.get("driverName"); //司机名称
	            	String driverPhone = params.get("driverPhone"); //司机电话
	            	String[] phoneNumberSet = new String[]{phoneNumber};
	            	String[] templateParamSet = new String[]{driverName, driverPhone};
	            	//发送短信
	            	Date sendTime = new Date();
	                String sendResult = sendSmsUtil.send(templateId, phoneNumberSet, templateParamSet);
	                if(PublicUtil.isEmpty(sendResult)){
	                	logger.info(driverStatus+"--------------------短信发送接口调用异常--------------------");
	                }else{
	                	Map sendStatusSetMap = formatSendResult(sendResult);
	                	String code = sendStatusSetMap.get("Code").toString();
	                	SmsInfoEntity smsInfoEntity = new SmsInfoEntity();//保存短信内容到短信信息记录表
	                	smsInfoEntity.setDataId(UUID.randomUUID().toString().replace("-", ""));
                        smsInfoEntity.setOrderNo(params.get("orderNo"));
                        smsInfoEntity.setSmsTemplate(templateId);
                        smsInfoEntity.setIsDelete(0);
                        smsInfoEntity.setIfNewVersion(1);
                        smsInfoEntity.setVersion(1);
                        smsInfoEntity.setCreateTime(sendTime);
                        smsInfoEntity.setUpdateTime(sendTime);
	                	if("Ok".equals(code)){ // 发送成功
	                        smsInfoEntity.setSendResult("1");
	                	 }else{
	                		 String message = sendStatusSetMap.get("Message").toString();
	                		 smsInfoEntity.setSendResult("0");
	                         smsInfoEntity.setReason(message);
	                	 }
	                	smsInfoService.insert(smsInfoEntity);
	                }
	            }else{
	            	logger.info(driverStatus+"--------------------代驾服务短信模板关闭，不发送--------------------");
	            }
			}else{
				logger.info(driverStatus+"--------------------代驾服务短信模板不存在--------------------");
			}
		}
	}
	
	private Map formatSendResult(String sendResult) {
        JSONObject jsonObject = JSONObject.parseObject(sendResult);
        JSONArray jsonArray = (JSONArray)jsonObject.get("SendStatusSet");
        JSONObject jsonObj = (JSONObject)jsonArray.get(0);
        return jsonObj.toJavaObject(Map.class);
    }
	
}
